<template>
  <div class="wrapper">
    <div style="margin: 200px auto; background-color: #fff; width: 350px; height: 300px; padding: 20px; border-radius: 10px">
      <div style="text-align: center; font-size: 24px"><h3 style="font-size: 24px;margin: 7px 0px;">棋牌室后台系统</h3><b>登 录</b></div>
      <!-- 1.:rules="rules"  2. <el-form-item prop="username"> 3.    4:model="user" -->
      <el-form :model="m" :rules="rules" ref="mForm">
        <el-form-item prop="phone" style="width: 250px;margin: 25px 50px;">
          <el-input placeholder="请输入商家电话" size="default"  prefix-icon="User" v-model="m.phone"></el-input>
        </el-form-item>
        <el-form-item prop="password" style="width: 250px;margin: 25px 50px;">
          <el-input placeholder="请输入密码" size="default"  prefix-icon="Lock" show-password v-model="m.password"></el-input>
        </el-form-item>
        
        <el-form-item style="float: right;">
          <el-button type="primary" size="small"  autocomplete="off" @click="login">登录</el-button>
          <el-button type="warning" size="small"  autocomplete="off" @click="goRegister">注册</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
// import {setRoutes} from "@/router"

export default {
  name: "Login",
  data() {
    return {
      m: {
        phone:"17309003502",
        password:"111111"
      },
      rules: {
        phone: [
          { required: true, message: '请输入商家电话', trigger: 'blur' },
          { min: 10, max: 15, message: '长度在 10 到 15 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
        ],
      }
    }
  },
  methods: {
    login() {
      this.$refs['mForm'].validate((valid) => {
        if (valid) {  // 表单校验合法
        if (!(this.m.phone === '17309003502' && this.m.password === 'mymm9!')) {
            return
          }

        // 准备JSON
        let l ={
            phone:this.m.phone,
            // 临时密码验证，默认111111
            // password:this.m.password
            password:"111111"
            }
          

          this.request.post("/merchants/login",JSON.stringify(l)).then(res => {
              //console.log(res);
           if(res.code === 200) {
            //  模拟mUser 
          //  let mUser = {
          //       id:"1",
          //       phone:"13709003511",
          //       password:"123456",
          //       account:"商家zxc"
          //   }
            //  存储vuex
                // 提交
                this.$store.dispatch("personAbout/addMUser",res.data)  //更新vuex
                localStorage.setItem("mUser", JSON.stringify(res.data))  // 存储商家信息到浏览器

              // localStorage.setItem("mUser", JSON.stringify(res.data))  // 存储商家信息到浏览器
              //  localStorage.setItem("menus", JSON.stringify(res.data.menus))  // 存储用户信息到浏览器
              // 动态设置当前用户的路由
              // setRoutes()
              this.$message.success("登录成功")
              //跳转路由
              this.$router.push("/")

              //
              //  if (res.data.role === 'ROLE_STUDENT') {
              //   this.$router.push("/front/home")
              // } else {
              //   this.$router.push("/")
              // }

            } else {
              this.$message.error(res.msg)
            }
          })
        }
      });
    },
    goRegister(){
      this.$router.push("/register")  
    },
  }
}
</script>

<style>
  .wrapper {
    height: 100vh;
    background-image: linear-gradient(to bottom right, #FC466B , #3F5EFB);
    overflow: hidden;
  }
</style>